<?php

namespace App\Http\Middleware;

use Closure;
use App\Log;


class LogToMysql
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        //$input = $request->all(); //操作的内容
        $path = $request->path();  //操作的路由
        $method = $request->method();  //操作的方法
        $ip = ip2long($request->ip());  //操作的IP
        $user = is_object($request->user()) ? $request->user()->name : '游客';

        $log = New Log();
        $log->name = $path;
        $log->path = $path;
        $log->method = $method;
        $log->user =  $user;
        $log->ip = $ip;
        $log->content = json_encode($request->all());
        $log->created_at = date("Y-m-d H:i:s");
        $log->updated_at = date("Y-m-d H:i:s");

        $log->save();

        return $next($request);
    }
}
